package data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import misc.Configurations;
import misc.Logger;

public class AuthorizationSession {
    //Query the Server Session State if user is registered (logged in)
    public static boolean checkAuthorizationToken(HttpServletRequest request,
                            HttpServletResponse response,boolean redirect)
    {
        try
        {
            HttpSession session = request.getSession();
            if(session.getAttribute("logged")!=null)
            {
                String token=(String)session.getAttribute("logged");
                if(token.equalsIgnoreCase("yes"));
                    return true;
            }

            if(redirect)
                response.sendRedirect(Configurations.DefaultPage+
                                        "?status=sessionfail");

            }
        catch(Exception e)
        {
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return false;
    }

    //Simple add a token to Server Session State
    public static void addAuthorizationToken(HttpSession session)
    {
        session.setAttribute("logged","yes");
    }
}
